<?php


namespace Admin\Model;
use Think\Model;

class TreestructModel extends Model{

        /**
         * 获取三级的uid
         * @return array(first,second,third) false 失败
         * @param 推荐人ID
         */    
        public function getpid_arr($uid)
        {
            if ($uid == 1) {
                return false;
            } else {
                $second_id = $this->field('pid')->where('id='.$uid)->find();
                if (empty($second_id)) {
                    return array('first'=>$uid);
                }
                $third_id = $this->field('pid')->where('id='.$second_id['pid'])->find();
                if (empty($third_id)) {
                    return array('first'=>$uid,'second'=>$second_id['pid']);
                }
                return array('first'=>$uid,'second'=>$second_id['pid'],'third'=>$third_id['pid']);
            }
        }
        /**
         * 获取推荐人ID 一级
         * @return 推荐人ID 或者false
         */
        public function getpid($uid)
        {
            if ($uid == 1) {
                return false;
            } else {
                $pid = $this->field('pid')->where('id='.$uid)->find();
                return $pid['pid'];
            }
        }

    /**
     * @param $uid
     * $return 代理ID
     */

    public function getagentid($uid)
    {
        $root_info = $this->where('id=1')->find();
        $par_info = $this->where('id='.$uid)->find();
        $map['lft'] = array('between',array($root_info['lft'],$par_info['lft']));
        $map['rgt'] = array('between',array($par_info['rgt'],$root_info['rgt']));
        $map['lvl'] = '3';
        $agent = $this->where($map)->find();
        return $agent;
    }
}